package com.jfinal.club._admin.exam;

import java.util.List;
import com.jfinal.club.common.model.Exam;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class ExamAdminService {

	private Exam dao = new Exam().dao();
	
	public Page<Exam> paginate(int pageNum) {
		return dao.paginate(pageNum, 10, "select exam.id,exam.ename,exam.extime,banji.clname,teacher.tname ", "from exam left join banji on exam.clid=banji.id left join teacher on exam.tid=teacher.id order by exam.id desc");
	}
	
	public boolean exists(int examId, String name) {
		name = name.toLowerCase().trim();
		String sql = "select id from exam where ename = ? and id != ? limit 1";
		Integer id = Db.queryInt(sql, name, examId);
		return id != null;
	}
	
	public List<Exam> getAll() {
		List<Exam> list = dao.find("select * from exam order by ename");
		return list;
	}
	
	public Ret save(int id, Exam exam) {
//		area.setId(id);
		exam.setEname(exam.getEname().trim());
		exam.save();
		return Ret.ok("msg", "创建成功");
	}
	
	public Exam edit(int id) {
		return dao.findById(id);
	}
	
	public Ret update(Exam exam) {
		if (exists(exam.getId(), exam.getEname())) {
			return Ret.fail("msg", "考试已经存在");
		}

		exam.setEname(exam.getEname().trim());
		exam.update();
		return Ret.ok("msg", "修改成功");
	}
	
	public Ret delete(int examId) {
		dao.deleteById(examId);
		return Ret.ok("msg", "考试删除成功");
	}

	
}
